/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controlenotas;

import java.awt.Color;
import java.awt.Component;
import java.math.BigDecimal;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

/**
 *
 * @author jeferson
 */
public class MCellRender extends DefaultTableCellRenderer{

    @Override
    public void setValue(Object value) {
/*        String s = (String)value;
        s = s.replace(",", ".");
        BigDecimal v = new BigDecimal(s);*/
        
        /*if(v.compareTo(new BigDecimal("5"))>=0)
            setForeground(Color.BLUE);
        else
            setForeground(Color.RED);*/
        //setHorizontalAlignment(JLabel.CENTER);
        //outras alterações entram aqui...
       /* try {
            String s = (String) value;
            //System.out.println(s);
            s = s.replace(",", ".");
            if (!s.equals("")) {
                BigDecimal v = new BigDecimal(s);
                if (v.compareTo(new BigDecimal("5")) >= 0) {
                    setForeground(Color.BLUE);
                } else {
                    setForeground(Color.RED);
                }
            }
        } catch(ClassCastException ex){
            
        } catch(NumberFormatException ex){
            
        }*/
        
        super.setValue(value);
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
            boolean hasFocus, int row, int column) {

        /*Component c = super.getTableCellRendererComponent(
                table,
                value,
                isSelected,
                hasFocus,
                row,
                column);*/



        JLabel c = new JLabel(""+value);
        c.setOpaque(true);
        //JTextField c = new JTextField(""+value);
       // c.setBorder(null);

        Color cb = new Color(240,240,240);
        /*if(row%2==0)
            c.setBackground(cb);*/

        Color cl = table.getSelectionBackground();
        cl = cl.brighter().brighter();
        if(!isSelected){
            if(column==0){
                setBackground(new Color(205, 205, 205));
                setForeground(Color.BLACK);
            }else{
                if(column==6 || column==8 || column==9 || column==11 || column==12 || column==14){
                    setBackground(new Color(230, 230, 230));
                }else{
                    if(row%2==0)
                        c.setBackground(cb);
                    else
                        c.setBackground(Color.WHITE);
                }
            }
        }else{
            if(hasFocus)
                c.setBackground(cl.brighter());
            else
                c.setBackground(cl);
        }

        //altera a aparência do componente baseado no valor da célula (value)
        //String s = (String) value;
        Color fc = c.getForeground();
        if(column>1){
            String s = (String)table.getValueAt(row, column);
            //System.out.println(s);
            s = s.replace(",", ".");
            if (!s.equals("")) {
                try{
                    BigDecimal v = new BigDecimal(s);
                    if (v.compareTo(new BigDecimal("5"))>=0) {
                        c.setForeground(Color.BLUE);
                    } else {
                        c.setForeground(Color.RED);
                    }
                }catch(NumberFormatException ex){
                    ex.printStackTrace();
                }
            } else
                c.setForeground(fc);
        }

        
        if(column!=1)
            c.setHorizontalAlignment(JLabel.CENTER);
        return c;
    }

}
